﻿using System.Web.Mvc;

namespace CodeContrib.Web.Mvc
{
	public class ModelNotFoundAttribute : ActionFilterAttribute
	{
		public override void OnResultExecuting(ResultExecutingContext filterContext)
		{
			var viewResult = filterContext.Result as ViewResult;

			if (viewResult != null
			    && viewResult.ViewData.Model == null)
			{
				filterContext.Result = new HttpNotFoundResult();
				filterContext.Result.ExecuteResult(filterContext.Controller.ControllerContext);
			}
		}
	}
}